package com.huxiu.utils;


import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.View;

import com.huxiu.R;

/**
 * Created by caoyuanjing on 15/12/14.
 */
public class Loading {
    public static Dialog mDialog;

    /**
     * 关闭dialog
     *
     * @param context
     */
    public static void showLoading(Context context) {
        try {
            if (mDialog != null) {
                mDialog.cancel();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        DialogInterface.OnKeyListener keyListener = new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode,
                                 KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_HOME
                        || keyCode == KeyEvent.KEYCODE_SEARCH) {
                    return true;
                }
                return false;
            }
        };

        try {
//            mDialog = new AlertDialog.Builder(context,R.style.loading).create();
            mDialog = new AlertDialog.Builder(context,R.style.loading).create();
            mDialog.setOnKeyListener(keyListener);
            mDialog.setCancelable(true);
            mDialog.setCanceledOnTouchOutside(true);
            mDialog.show();
            mDialog.setContentView(R.layout.loading);
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    /**
     * 关闭dialog
     *
     */
    public static void closeLoading(){
        if (mDialog != null) {
            try {
                mDialog.dismiss();
                mDialog = null;
            } catch (Exception e) {
            }

        }
    }


}
